package egovframework.npqs.pqi.mobile;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.slidingmenu.lib.app.SlidingFragmentActivity;

import egovframework.npqs.pqi.mobile.menu.MenuControl;

public abstract class BaseActivity extends SlidingFragmentActivity {
		
	public void addFragment(int layoutId, BaseFragment baseFragment, String name) {		
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.replace(layoutId, baseFragment, name);
		ft.addToBackStack(null);
		ft.commit();			
		
		
	}	
	
	public void addFragmentReplace(int layoutId, BaseFragment baseFragment, String name) {
		MenuControl.tagetFragment = baseFragment;
		//clearBackStack();		
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.replace(layoutId, baseFragment, name);
		ft.commit();			
	}	
	
	public void removeFragment(BaseFragment baseFragment) {		
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.remove(baseFragment);
		ft.commit();
		
	}		

	public int getBackStackCount(){
	    FragmentManager fragmentManager = getSupportFragmentManager();	    
	    int count = fragmentManager.getBackStackEntryCount();
	    return count;    
	}	
	
	public void clearBackStack(){
		FragmentManager fragmentManager = getSupportFragmentManager();
		fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
		/*
		for(int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) {            
			fragmentManager.popBackStackImmediate();			
		}    
		*/
	}
	
	@Override
	public void onBackPressed() {
		//finish();
	}
	
	public BaseFragment findFragment(String fragmentTag) { 
		FragmentManager fragmentManager = getSupportFragmentManager();
		android.support.v4.app.Fragment frament = fragmentManager.findFragmentByTag(fragmentTag);
		
		if(frament != null) {
			return (BaseFragment) frament;
		} else {
			return null;
		}	    		
	}
	
	protected android.support.v4.app.Fragment getVisibleFragment(int flag){
	      try{
	           FragmentManager fragmentManager = getSupportFragmentManager();
	             List<android.support.v4.app.Fragment> fragments = fragmentManager.getFragments();
	              if(fragments != null){
	                for(int i = fragments.size() ; i > 0 ; i--){
	                      if(fragments.get(i-1) != null){
	                         return fragments.get(i-1);
	                      }
	                   }
	                }
	                /*  for(android.support.v4.app.Fragment fragment : fragments){
	                       if(fragment != null && fragment.isVisible())
	                           return fragment;
	                   }*/
	      }catch(Exception e){
	         e.printStackTrace();
	      }
	       return null;
	}
	
	protected Fragment getFragmentTag(String tag){
		FragmentManager fragmentManager = getSupportFragmentManager();
		Fragment fragment = fragmentManager.findFragmentByTag(tag);
		return fragment;
	}	
	
}
